<script setup lang="ts">
import { nextTick, onMounted, reactive, ref, watch, onBeforeUnmount } from 'vue'
import {
  deleteAttr,
  getCategory,
  saveAttrInfo,
} from '@/api/product/attr/index.ts'
import useStoreCategory from '@/store/modules/category.ts'
import { ElMessage } from 'element-plus'
const storeCategory = useStoreCategory()
let attrArr = ref([])
let screen = ref(1)
let attrParams = reactive<any>({
  attrName: '', //新增的属性的名字
  attrValueList: [
    //新增的属性值数组
  ],
  categoryId: '', //三级分类的ID
  categoryLevel: 3, //代表的是三级分类
})
let inputArr = ref<any>([])
onMounted(() => {})
watch(
  () => storeCategory.c3Id,
  () => {
    attrArr.value = []
    if (!storeCategory.c3Id) return
    getArr()
  },
)
const getArr = async () => {
  const { c1Id, c2Id, c3Id } = storeCategory
  let res = await getCategory(c1Id, c2Id, c3Id)
  if (res.code == 200) {
    attrArr.value = res.data
  }
}
const addAttr = () => {
  screen.value = 0
  Object.assign(attrParams, {
    attrName: '',
    attrValueList: [],
    categoryId: storeCategory.c3Id,
    categoryLevel: 3,
  })
}
const editAttr = (row: any) => {
  screen.value = 0
  Object.assign(attrParams, JSON.parse(JSON.stringify(row)))
}
const deleteAttrs = async (id: number) => {
  let res: any = await deleteAttr(id)
  if (res.code == 200) {
    ElMessage({
      type: 'success',
      message: '删除成功',
    })
    getArr()
  } else {
    ElMessage({
      type: 'error',
      message: '删除失败',
    })
  }
}
const addAttrValue = () => {
  attrParams.attrValueList.push({ valueName: '', off: true })
  nextTick(() => {
    inputArr.value[attrParams.attrValueList.length - 1].focus()
  })
}
const cancel = () => {
  screen.value = 1
}
const save = async () => {
  let res: any = await saveAttrInfo(attrParams)
  if (res.code == 200) {
    screen.value = 1
    ElMessage({
      type: 'success',
      message: attrParams.id ? '修改成功' : '添加成功',
    })
    getArr()
  } else {
    ElMessage({
      type: 'error',
      message: attrParams.id ? '修改失败' : '添加失败',
    })
  }
}
const blurInput = (row: any, index: number) => {
  if (row.valueName.trim() == '') {
    attrParams.attrValueList.splice(index, 1)
    ElMessage({
      type: 'error',
      message: '属性值不能为空',
    })
    return
  }
  let repeat = attrParams.attrValueList.find((item: any) => {
    if (item != row) {
      return item.valueName === row.valueName
    }
  })

  if (repeat) {
    attrParams.attrValueList.splice(index, 1)
    //提示信息
    ElMessage({
      type: 'error',
      message: '属性值不能重复',
    })
    return
  }
  row.off = false
}
const changOff = (row: any, index: number) => {
  row.off = true
  nextTick(() => {
    inputArr.value[index].focus()
  })
}
onBeforeUnmount(() => {
  storeCategory.$reset()
})
</script>

<template>
  <Category :screen="screen"></Category>
  <el-card style="margin-top: 10px">
    <div v-show="screen != 0">
      <el-button
        type="primary"
        icon="Plus"
        plain
        @click="addAttr"
        :disabled="!storeCategory.c3Id"
        v-has="`btn.Attr.add`"
      >
        添加平台属性
      </el-button>
      <el-table :data="attrArr" style="margin: 10px 0" border>
        <el-table-column
          type="index"
          label="序号"
          width="80"
          align="center"
        ></el-table-column>
        <el-table-column prop="attrName" label="属性名称"></el-table-column>
        <el-table-column label="属性值名称">
          <template #default="scope">
            <el-tag
              style="margin: 5px"
              v-for="item in scope.row.attrValueList"
              :key="item.id"
            >
              {{ item.valueName }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button
              type="warning"
              icon="Edit"
              plain
              @click="editAttr(scope.row)"
              v-has="`btn.Attr.update`"
            ></el-button>
            <el-popconfirm
              :title="`你确定删除'${scope.row.attrName}'吗？`"
              @confirm="deleteAttrs(scope.row.id)"
              width="250"
            >
              <template #reference>
                <el-button
                  type="danger"
                  icon="Delete"
                  plain
                  v-has="`btn.Attr.remove`"
                ></el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div v-show="screen != 1">
      <el-form :inline="true">
        <el-form-item label="属性名称">
          <el-input
            v-model="attrParams.attrName"
            placeholder="请你输入属性名称"
          ></el-input>
        </el-form-item>
      </el-form>
      <el-button
        @click="addAttrValue"
        :disabled="!attrParams.attrName"
        type="primary"
        size="default"
        icon="Plus"
      >
        添加属性值
      </el-button>
      <el-button type="primary" size="default" @click="cancel">取消</el-button>
      <el-table border style="margin: 10px 0" :data="attrParams.attrValueList">
        <el-table-column
          label="序号"
          width="80px"
          type="index"
          align="center"
        ></el-table-column>
        <el-table-column label="属性值名称">
          <template #default="scope">
            <el-input
              :ref="(vc: any) => (inputArr[scope.$index] = vc)"
              v-if="scope.row.off"
              @blur="blurInput(scope.row, scope.$index)"
              v-model="scope.row.valueName"
              placeholder="请输入属性值名称"
            ></el-input>
            <div v-else @click="changOff(scope.row, scope.$index)">
              {{ scope.row.valueName }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="属性值操作">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              icon="Delete"
              @click="attrParams.attrValueList.splice(scope.$index, 1)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-button
        type="primary"
        size="default"
        :disabled="attrParams.attrValueList.length <= 0"
        @click="save"
      >
        保存
      </el-button>
      <el-button type="primary" size="default" @click="cancel">取消</el-button>
    </div>
  </el-card>
</template>

<style scoped lang="scss"></style>
